import 'package:aoe_flutter/mvvm/bundle/bundle.dart';
import 'package:flutter/material.dart';
import './widget_item.dart';

class WidgetItemContainer extends StatelessWidget {
  final int columnCount; //一行几个
  final List<dynamic> bundles;
  final bool isWidgetPoint;

  // 所有的可用demos;
  WidgetItemContainer(
      {Key key,
      @required this.bundles,
      @required this.columnCount,
      @required this.isWidgetPoint})
      : super(key: key);

  List<Widget> _buildColumns(context) {
    List<Widget> _listWidget = [];
    List<Widget> _listRows = [];
    int addI;
    for (int i = 0, length = bundles.length; i < length; i += columnCount) {
      _listRows = [];
      for (int innerI = 0; innerI < columnCount; innerI++) {
        addI = innerI + i;
        if (addI < length) {
          Bundle item = bundles[addI];
          _listRows.add(
            Expanded(
              flex: 1,
              child: WidgetItem(
                widgetIcon: item.icon,
                title: item.name,
                onTap: () {
                  Navigator.of(context)
                      .push(MaterialPageRoute(builder: (context) => item));
                },
                index: addI,
                totalCount: length,
                rowLength: columnCount,
                textSize: isWidgetPoint ? 'middle' : 'small',
              ),
            ),
          );
        } else {
          _listRows.add(
            Expanded(
              flex: 1,
              child: Container(),
            ),
          );
        }
      }
      _listWidget.add(
        Row(
          children: _listRows,
        ),
      );
    }
    return _listWidget;
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: _buildColumns(context),
    );
  }
}
